package cl.rizepe.playntest.core.effects;

import static playn.core.PlayN.graphics;
import playn.core.Image;
import pythagoras.f.Point;
import cl.rizepe.playntest.core.views.EffectedLayer;

public class EffectPaint extends EffectLayerBlank{
	int position, columns, rows;
	Image bgImg;
	
	public EffectPaint(EffectedLayer layerState, int position, int columns, int rows, Image image) {
		super(layerState);
		this.columns=columns;
		this.rows=rows;
		this.position=position;
		//drawShape();
		bgImg=image;
		shape=graphics().createImageLayer(graphics().createImage(1,1));
		shape.setVisible(false);
	}
	
	

	protected void drawShape() {
		if (shape!=null)
			shape.destroy();
	
		shape=null;
	//	pythagoras.f.Transform t = layerState.getImageLayer().transform();
		
		shape=graphics().createImageLayer(bgImg);
		Point translate = layerState.getTranslate();
		shape.setTranslation(translate.x(), translate.y());		
		shape.setScale(scale());
		shape.setRotation(layerState.getRotation());
		if (layerState.parent()!=null)
			layerState.parent().add(shape);
	}
	
	protected float scale(){
		float maxWidth=layerState.scaledWidth()/columns;
		float maxHeight=layerState.scaledHeight()/rows;
		float layerAspectQuotient=maxWidth/maxHeight;
		float paintAspectQuotient = shape.width()/shape.height();
		Point size = layerState.getSize();
		float originX= size.x()*columns/2;
		float originY= size.y()*rows/2;
		originX-=size.x()*((float)(position%columns));
		originY-=size.y()*((float)(position/columns));
		
		shape.setOrigin(originX,originY);
		//shape.setOrigin(,);
		//shape.setOrigin(0, 0);
		//shape.setOrigin(50, 50);
		if (layerAspectQuotient>=1)
			if (paintAspectQuotient>=1)
				return maxWidth/shape.width();
			else
				return maxWidth/shape.width();
		//implementar
		else
			if (paintAspectQuotient<1)
				return maxWidth/shape.width();
		//corregir
			else
				return maxWidth/shape.width();
		//corregir
			
	}
/*
 		if (shape!=null)
			shape.destroy();
		float diameter =(float) Math.sqrt(layerState.getLayer().height()*layerState.getLayer().height()+layerState.getLayer().width()*layerState.getLayer().width());
		CanvasImage canvasImage =graphics().createImage(diameter*2,diameter*2);
		shape=graphics().createImageLayer(canvasImage);
		pythagoras.f.Transform t = layerState.getLayer().transform();
		shape.setOrigin(0,0);
		//shape.setTranslation(t.tx()-layer.width()/2, t.ty()-layer.height()/2);
		shape.setTranslation(t.tx()-diameter/2, t.ty()-diameter/2);
		Canvas canvas;
		canvas=canvasImage.canvas();
		canvas.clear();
		canvas.setFillColor(color);		
		canvas.setAlpha(alpha);
		canvas.fillCircle(diameter/2, diameter/2, diameter/2);
		graphics().rootLayer().add(shape);
 * */	

}
